package com.ljm.service;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

import java.util.Map;

/**
 * @author ChenHuaMing
 * 版权：    Copyright by ljm
 * 描述：
 * 修改人：  HuamingChen
 * 修改时间：2020/5/4
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */
public abstract class BaseServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> {
    private static final Logger logger= LoggerFactory.getLogger(BaseServiceImpl.class);

    protected static MapperFacade mapperFacade;
    protected static MapperFactory mapperFactory=new DefaultMapperFactory.Builder().build();

    public BaseServiceImpl() {
        this.intMapperFacade();
    }

    /**
     * 转换配置
     * @return 转换规则
     */
    public abstract Map<Class,Class> classMap();

    private void intMapperFacade() {
        Map<Class, Class> classClassMap = this.classMap();
        if(!CollectionUtils.isEmpty(classClassMap)){
            for(Map.Entry entry:classClassMap.entrySet()){
                mapperFactory.classMap((Class)entry.getKey(), (Class)entry.getValue()).byDefault().register();
            }
            mapperFacade=mapperFactory.getMapperFacade();
        }
    }

}
